<?php
//ob_start();
// tao code random
//$text = substr(str_shuffle($alphanum), 0, 5);
// Begin the session abcdefghjkmnpqrstuvwxyz
session_start();
$text = $_SESSION['security_code'] = strtoupper(substr(str_shuffle('1234567abcdefghjkmnpqrstuvwxyz'), 0, 4));

// If the session is not present, set the variable to an error message
//if(!isset($_SESSION['security_code']))
//      $text = 'ERROR!';
// Else if it is present, set the variable to the session contents
//else
//      $text = $_SESSION['security_code'];

// chon random background
$bgnum = rand(1, 7);

// chua dinh nghia size thi size la 14
if(!isset($sz))
        $sz = 16;

// duong dan file font
$fontfile = $_SERVER["DOCUMENT_ROOT"]."/static/css/arial.ttf";

// kich thuoc anh can de chua chuoi text voi textsize la sz
// bounding box cua text su dung TrueType font
// 0    lower left corner, X position
// 1    lower left corner, Y position
// 2    lower right corner, X position
// 3    lower right corner, Y position
// 4    upper right corner, X position
// 5    upper right corner, Y position
// 6    upper left corner, X position
// 7    upper left corner, Y position
$textsize = imagettfbbox($sz, 0, $fontfile, $text);
//var_dump($textsize);die;
// kich thuoc cac chieu cua text
$dx = abs($textsize[2] - $textsize[0]);
$dy = abs($textsize[5] - $textsize[3]);

// padding
$xpad = 15;
$ypad = 15;

// file anh source
$filename = $_SERVER["DOCUMENT_ROOT"]."/static/images/securityimage$bgnum.jpg";


// tao anh tu background
$source = imagecreatefromjpeg($filename);
//var_dump($filename);die;
if($source)
{
        // kich thuoc anh
        list($imagewidth, $imageheight) = getimagesize($filename);

        // tao mot anh moi voi kich thuoc bounding box cua text + padding
        $image = imagecreatetruecolor($dx + $xpad, $dy + $ypad);

        // resize
        imagecopyresized($image, $source, 0, 0, 0, 0, $dx + $xpad, $dy + $ypad, $imagewidth, $imageheight);
}
else
{
        $image = imagecreatetruecolor($dx + $xpad, $dy + $ypad);
}

// create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 0x2c, 0x6D, 0xAF);

// tao khung
imagerectangle($image, 0, 0, $dx + $xpad - 1, $dy + $ypad - 1, $blue);
imagerectangle($image, 0, 0, $dx + $xpad, $dy + $ypad, $blue);

if(rand(1, 2) == 1)
{
        $angle = rand(5, 7);
        // tao shadow
        imagettftext($image, $sz, $angle, (int)($xpad / 2) + 1, $dy + (int)($ypad / 2) + 4, $grey, $fontfile, $text);

        // add text
        imagettftext($image, $sz, $angle, (int)($xpad / 2), $dy + (int)($ypad / 2) - 1 + 4, $black, $fontfile, $text);
}
else
{
        $angle = 360 - rand(5, 7);
        // tao shadow
        imagettftext($image, $sz, $angle, (int)($xpad / 2) + 1, $dy + (int)($ypad / 2) - 2, $grey, $fontfile, $text);

        // add text
        imagettftext($image, $sz, $angle, (int)($xpad / 2), $dy + (int)($ypad / 2) - 1 - 2, $black, $fontfile, $text);
}

// Luu code random vao session
//$_SESSION['verificationcode'] = md5($text);


// Yeu cau expired la 1 ngay trong qua khu
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// Luon luon la da modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// Khong cho cache
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");

// Gui anh xuong trinh duyet
header('Content-type: image/jpeg');

// Chuyen anh xuong trinh duyet
imagejpeg($image);

// Giai phong bo nho
imagedestroy($image);
//ob_end_flush();
?>